package qiurank;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2016/1/8.
 */
public class SameWordModel extends Object{
    private DB.Table sameworddb;
    public SameWordModel(){
        sameworddb=DB.table("sameword");
    }
    public String replaceContent(String content){
        ResultSet rs=sameworddb.orderBy("id","asc").take(1).get();

        int i=1;
        while (true){
            try {
                if(rs==null){
                    break;
                }

                if(rs.next()) {
                    String keyword=rs.getString("keyword");
                    String keyword2=rs.getString("keyword2");
                    Pattern pattern=Pattern.compile(keyword);
                    Matcher matcher=pattern.matcher(content);
                    Pattern pattern2=Pattern.compile(keyword2);
                    Matcher matcher2=pattern.matcher(content);


                    if(matcher.find()){
                        content=content.replaceAll(keyword,keyword2);
                        break;
                    }else if(matcher2.find()){
                        content=content.replaceAll(keyword2,keyword);
                        break;
                    }

                    rs=sameworddb.orderBy("id","asc").skip(i).take(1).get();
                    i++;
                }else {
                    break;
                }

            } catch (SQLException e) {
                e.printStackTrace();
                break;
            }

        }
        return content;
    }
}
